//
//  UpdatePasswordViewController.m
//  DA_V
//
//  Created by 周季阳 on 15/4/22.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "UpdatePasswordViewController.h"
#import "InterFace.h"
#import "UIViewAdditions.h"
#import "UpdateSuccessViewController.h"
#import "LoginData.h"
@interface UpdatePasswordViewController ()<UITextFieldDelegate,LoginDelegate>
{
    UITextField *writePhoneNum;
    UITextField *writePhoneNum1;
}
@end

@implementation UpdatePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [self colorWithHexString:@"#F5F5F5"];
    [self initNav];
    navTitle.text = @"重置密码";
    
    [self initUI];
}
#pragma mark - initUI
-(void)initUI{
    
    UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, navView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT*0.6)];
    backgroundView.backgroundColor = [UIColor whiteColor];
    backgroundView.userInteractionEnabled = YES;
    [self.view addSubview:backgroundView];
    
    //    UIImageView *loginPwdB = [[UIImageView alloc] initWithFrame:CGRectMake(30,50, SCREEN_WIDTH-60, SCREEN_HEIGHT*0.07)];
    //    //loginPwdBG.image = [UIImage imageNamed:@"updatepassword_03"];
    //    loginPwdB.backgroundColor = [UIColor whiteColor];
    //    loginPwdB.layer.borderColor = [UIColor grayColor].CGColor;
    //    loginPwdB.layer.borderWidth = 0.5;
    //    loginPwdB.userInteractionEnabled = YES;
    //    [backgroundView addSubview:loginPwdB];
    //
    //
    //    UITextField *writeOldNum = [[UITextField alloc] initWithFrame:CGRectMake(10, 0 ,loginPwdB.frame.size.width-20 , SCREEN_HEIGHT*0.07)];
    //    writeOldNum.tag = 1;
    //    writeOldNum.placeholder = @"原密码";
    //    writeOldNum.font = [UIFont systemFontOfSize:14];
    //    //writePhoneNum.backgroundColor= [UIColor whiteColor];
    //    [loginPwdB addSubview:writeOldNum];
    //    writeOldNum.delegate = self;
    
    
    UIImageView *loginPwdBG = [[UIImageView alloc] initWithFrame:CGRectMake(30,50, SCREEN_WIDTH-60, SCREEN_HEIGHT*0.07)];
    //loginPwdBG.image = [UIImage imageNamed:@"updatepassword_03"];
    loginPwdBG.backgroundColor = [UIColor whiteColor];
    loginPwdBG.layer.borderColor = [UIColor grayColor].CGColor;
    loginPwdBG.layer.borderWidth = 0.5;
    loginPwdBG.userInteractionEnabled = YES;
    [backgroundView addSubview:loginPwdBG];
    
    
    writePhoneNum = [[UITextField alloc] initWithFrame:CGRectMake(10, 0 ,loginPwdBG.frame.size.width-20 , SCREEN_HEIGHT*0.07)];
    writePhoneNum.tag = 2;
    writePhoneNum.placeholder = @"新密码";
    writePhoneNum.font = [UIFont systemFontOfSize:14];
    //writePhoneNum.backgroundColor= [UIColor whiteColor];
    [loginPwdBG addSubview:writePhoneNum];
    writePhoneNum.delegate = self;
    
    UIImageView *loginPwdBG1 = [[UIImageView alloc] initWithFrame:CGRectMake(30,loginPwdBG.bottom+10, SCREEN_WIDTH-60, SCREEN_HEIGHT*0.07)];
    //loginPwdBG1.image = [UIImage imageNamed:@"updatepassword_03"];
    loginPwdBG1.backgroundColor = [UIColor whiteColor];
    loginPwdBG1.layer.borderColor = [UIColor grayColor].CGColor;
    loginPwdBG1.layer.borderWidth = 0.5;
    loginPwdBG1.userInteractionEnabled = YES;
    [backgroundView addSubview:loginPwdBG1];
    
    
    writePhoneNum1 = [[UITextField alloc] initWithFrame:CGRectMake(10, 0 ,loginPwdBG.frame.size.width-20 , SCREEN_HEIGHT*0.07)];
    writePhoneNum1.tag = 3;
    writePhoneNum1.placeholder = @"确认密码";
    writePhoneNum1.font = [UIFont systemFontOfSize:14];
    //writePhoneNum.backgroundColor= [UIColor whiteColor];
    [loginPwdBG1 addSubview:writePhoneNum1];
    writePhoneNum1.delegate = self;
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(20, loginPwdBG1.bottom+80, SCREEN_WIDTH/2-30, SCREEN_WIDTH*0.11);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"sure"] forState:UIControlStateNormal];
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"sure1"] forState:UIControlStateHighlighted];
    [leftBtn addTarget:self action:@selector(updatePassword) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView addSubview:leftBtn];
    
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(leftBtn.right+20, loginPwdBG1.bottom+80, SCREEN_WIDTH/2-30, SCREEN_WIDTH*0.11);
    [rightBtn setBackgroundImage:[UIImage imageNamed:@"cancel"] forState:UIControlStateNormal];
    [rightBtn setBackgroundImage:[UIImage imageNamed:@"cancel1"] forState:UIControlStateHighlighted];
    [rightBtn addTarget:self action:@selector(clickRightBtn) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView addSubview:rightBtn];
    UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)];
    [singleTapGestureRecognizer setNumberOfTapsRequired:1];
    [backgroundView addGestureRecognizer:singleTapGestureRecognizer];
    
}
-(void)singleTap{
    [writePhoneNum resignFirstResponder];
    [writePhoneNum1 resignFirstResponder];
}
#pragma mark - UItextField Delegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
#pragma mark - 确认
-(void)updatePassword{
    UITextField *text1 = (UITextField *)[self.view viewWithTag:2];
    UITextField *text2 = (UITextField *)[self.view viewWithTag:3];
    if (text1.text == nil ||text1.text.length<=0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请输入新密码" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    if (text2.text == nil ||text2.text.length<=0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请再次输入新密码" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    LoginData *login = [[LoginData alloc]init];
    login.delegate = self;
    [login updataPassword:[[NSUserDefaults standardUserDefaults] objectForKey:@"userID"] andNewLoginPass:text1.text andReNewLoginPass:text2.text];
}
#pragma mark - 重置密码回调
-(void)updataPassword:(NSDictionary *)sign{
    if ([[sign objectForKey:@"status"] isEqualToString:@"success"]) {
    UpdateSuccessViewController *success = [[UpdateSuccessViewController alloc]init];
    [self.navigationController pushViewController:success animated:YES];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[sign objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];

    }
}
#pragma mark - 重置密码回调失败
-(void)requistFail{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}
#pragma mark - 取消
-(void)clickRightBtn{
    
}
#pragma mark - 返回上一个界面
-(void)clickBackBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
